<template>
	<view class="profile">
		<view class="top">
			<view class="cover">
				<image class="img" :src="userInfo ? userInfo.img : userImg"></image>
			</view>
			<text class="name" v-if="userInfo">{{ userInfo.nickname}}</text>
			<text class="name" @click="login" v-else>请去登录</text>
		</view>

		<view class="card">
			<icon-list style="width: 100%;" @select="select" :list="listIcon"></icon-list>
		</view>

		<!-- <view class="card">
			<view class="card-title">
				<uni-title title="书架" type="h3" style="margin-bottom: 10px;"></uni-title>
				<text style="color: #007AFF;" @click="toShelf">查看</text>
			</view>

			<scroll-list :list="shelfBooks" @scrollClick="scrollClick"></scroll-list>
		</view> -->

		<show-card class="card" v-if="shelfBooks !=null && shelfBooks.length > 0" title="书架" :list="shelfBooks"
			desc="更多" @more="toShelf"></show-card>

		<button class="btn" @click="logout" v-if="userInfo" type="primary">退出登录</button>

	</view>
</template>

<script>
	import userImg from '@/static/user.jpg'
	import utils from '@/static/js/utils.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'

	export default {
		data() {
			return {
				listIcon: [{
					icon: "hand-up",
					text: "我的点赞",
					isSelected: false,
					id: "likes",
					color: "#22ccff"
				}, {
					icon: "star",
					text: "我的收藏",
					isSelected: false,
					id: "collection",
					color: "#22ccff"
				}, {
					custom: "icon-lishi",
					text: "我的历史",
					isSelected: false,
					id: "history",
					color: "#22ccff"
				}],
				userImg: userImg,
				shelfBooks: null
			}
		},
		computed: {
			...mapState([
				'userInfo'
			])
		},
		methods: {

			select(id) {
				if (id === "collection") {
					this.toShelf()
				}
			},

			//对象展开运算符直接拿到mutations
			...mapMutations(['setUserInfo']),
			logout() {
				utils.http.removeTokenAndExpireTime()
				// utils.globalData.setUserInfo(null)
				this.shelfBooks = null
				this.setUserInfo(null)
			},
			toShelf() {
				uni.navigateTo({
					url: "/pages/shelf/shelf"
				})
			},
			login() {
				uni.navigateTo({
					url: "/pages/login/login"
				})
			},
			shelfLoad() {
				let that = this;
				utils.http.get(
					"/shelf/profileShelf"
				).then(res => {
					if (res.status == 200) {
						this.shelfBooks = res.data
					}
				});
			},
			scrollClick(id) {
				uni.navigateTo({
					url: '/pages/contents/contents?id=' + id
				})
			},
		},
		onShow() {
			this.shelfLoad()
		}
	}
</script>

<style>
	.profile {
		height: 100%;
		background-color: #FaFaFa;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.top {
		width: 90%;
		margin: 30px 20px;
		margin-bottom: 20px;
		padding: 10px 10px;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		flex-shrink: 0;
		height: 55px;
	}

	.cover {
		margin-right: 10px;
		width: 50px;
		height: 50px;
	}

	.name {
		font-weight: 500;
		font-size: 20px;
		color: #007AFF;
	}


	.img {
		width: 50px;
		height: 50px;
		border-radius: 50% 50%;
	}

	.card {
		margin: 10px 0px;
		padding: 10px 20px;
		background-color: white;
		border-radius: 10px;
		width: 85%;
	}

	.card-title {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.card-more {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 10px 0;
	}

	.btn {
		width: 80%;
		margin: 10px 0;
		border-radius: 25px 25px;
	}
</style>
